﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Utils;

namespace Filters;

/// <summary>
/// 手机验证码
/// </summary>
public class PhoneCodeAttribute : Attribute, IAsyncActionFilter
{
    /// <summary>
    /// 手机验证码
    /// </summary>
    /// <param name="context"></param>
    /// <param name="next"></param>
    /// <returns></returns>
    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        var querys = context.HttpContext.Request.Query;

        if (!querys.ContainsKey("phone") || !querys.ContainsKey("code"))
        {
            context.Result = new JsonResult(new { code = 200, msg = "缺少参数", state = false });

            return;
        }

        var phone = querys["phone"].ToString();

        var code = querys["code"].ToString();

        await PhoneCode.Valid(phone, code);

        await next();
    }
}
